import json, os
import requests
import uuid
from pathlib import Path

def dd_robot(msg):
    # 钉钉通知
    # msg += "\n当前时间为：" + datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    print(msg)
    HEADERS = {"Content-Type": "application/json;charset=utf-8"}
    url = "https://oapi.dingtalk.com/robot/send?access_token=99d27d8c6f910fc4db2b1b0eb7583d5d8d1f4ee9599c630b3af7a29b95c9be97"

    data_info = {
        "msgtype": "text",
        "text": {
        "content": "炉石" + msg
        },
        "isAtAll": False
    }

    value = json.dumps(data_info)
    requests.post(url,data=value,headers=HEADERS)

def read_json(f_dir):
    # 读取json文件
    contents = Path(f_dir).read_text()
    data = json.loads(contents)
    return data

def get_SavedAccountNames(f_dir):
    # 获得当前所有登陆过的账户
    data = read_json(f_dir)
    SavedAccountNames = str(data['Client']['SavedAccountNames']).split(',')
    # print(SavedAccountNames)
    return SavedAccountNames

def save_SavedAccountNames(f_dir, accounts):
    # 对json进行修改
    data = read_json(f_dir)
    s = ""
    for i in range(len(accounts)):
        if (i != 0):
            s += ','
        s += accounts[i]
    print(s)
    data['Client']['SavedAccountNames'] = s
    with open(f_dir,'w',encoding='utf-8') as f:
        f.write(json.dumps(data,ensure_ascii=False,indent=1))

def get_now_account_local(f_dir):
    # 获得战网目前登陆的账户
    return get_SavedAccountNames(f_dir)[0]

def kill_progess_win(task_name):
    # windows结束进程函数 task_name 为进程任务名称
    os.system('taskkill /f /t /im "'+ task_name + '"')

def change_account(f_dir, account):
    kill_progess_win("lsd.exe")
    # 判断是否需要切换账户
    if (get_now_account_local(f_dir) == account):
        print("不需要切换账号")
        return True
    if (account == "-1"):
        print("没有账号，请去后台设置挂机账号")
        return False
    # 切换账户
    data = read_json(f_dir)
    accounts = get_SavedAccountNames(f_dir)
    if (account in accounts):
        # 如果账户登陆过这个电脑则切换登陆
        i = accounts.index(account)
        accounts[0], accounts[i] = accounts[i], accounts[0]
        save_SavedAccountNames(f_dir, accounts)
        print("切换账号成功")
        # 清理进程
        kill_progess_win("Battle.net.exe")
        kill_progess_win("Hearthstone.exe")

        return True
    else :
        # 未登陆过
        print("请先手动登陆一次")
        return False

def add_account(f_dir, account):
    data = read_json(f_dir)
    accounts = []
    accounts.append(account)
    if (account in get_SavedAccountNames(f_dir)):
        accounts.append(account)
    save_SavedAccountNames(f_dir, accounts)

def get_account_info(computer_name, url):
    # 调用云函数 获取当前应该挂机的账户信息
    name = uuid.uuid3(uuid.NAMESPACE_DNS, computer_name)
    HEADERS = {"Content-Type": "application/json"}
    data_info = {
        "computer_name": str(name),
    }
    value = json.dumps(data_info)
    re = requests.post(url,data=value,headers=HEADERS)
    print("云函数返回：" + re.text)
    return re.text

def get_mac_address():
    # 获取mac地址
    node = uuid.getnode()
    mac = uuid.UUID(int = node).hex[-12:]
    return mac